using System;
using System.Collections.Generic;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;

namespace NAGFwk.UI
{
    public class ZoomManager
    {
        int _zoomFactor = 0;
        readonly List<int> _zoomValues = new List<int>() { 100, 115, 130, 150, 200, 250, 300, 350, 400, 500 };
        readonly RibbonControl _ribbon;
        readonly BarEditItem _beiZoom;
        readonly ModulesNavigator _modulesNavigator;
        public ZoomManager(RibbonControl ribbon, ModulesNavigator modulesNavigator, BarEditItem beItem)
        {
            this._ribbon = ribbon;
            this._modulesNavigator = modulesNavigator;
            this._beiZoom = beItem;
            this._beiZoom.HiddenEditor += this.beiZoom_HiddenEditor;
            this._beiZoom.ShownEditor += this.beiZoom_ShownEditor;
        }

        private ZoomTrackBarControl ZoomControl { get; set; }

        public int ZoomFactor
        {
            get { return _zoomFactor; }
            set
            {
                _zoomFactor = value;
                _beiZoom.Caption = string.Format(" {0}%", ZoomFactor);
                int index = _zoomValues.IndexOf(ZoomFactor);
                if (index == -1)
                    _beiZoom.EditValue = ZoomFactor / 10;
                else
                    _beiZoom.EditValue = 10 + index;
                _modulesNavigator.CurrentModule.ZoomFactor = (float)ZoomFactor / 100;
            }
        }
        public void SetZoomCaption(string caption)
        {
            _beiZoom.Caption = caption;
        }
        private void beiZoom_ShownEditor(object sender, ItemClickEventArgs e)
        {
            this.ZoomControl = _ribbon.Manager.ActiveEditor as ZoomTrackBarControl;
            if (ZoomControl != null)
            {
                ZoomControl.ValueChanged += OnZoomTackValueChanged;
                OnZoomTackValueChanged(ZoomControl, EventArgs.Empty);
            }
        }
        private void beiZoom_HiddenEditor(object sender, ItemClickEventArgs e)
        {
            ZoomControl.ValueChanged -= OnZoomTackValueChanged;
            this.ZoomControl = null;
        }
        private void OnZoomTackValueChanged(object sender, EventArgs e)
        {
            int val = val = ZoomControl.Value * 10;
            if (ZoomControl.Value > 10)
                val = _zoomValues[ZoomControl.Value - 10];
            ZoomFactor = val;
        }
    }
}
